package com.innovative.androiddemo1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.innovative.androiddemo1.cls.Addresses;
import com.innovative.androiddemo1.cls.Categories;
import com.innovative.androiddemo1.cls.CategoryDishes;
import com.innovative.androiddemo1.cls.Dishes;
import com.innovative.androiddemo1.cls.Item_Cat;
import com.innovative.androiddemo1.cls.Item_CatDish;
import com.innovative.androiddemo1.cls.Item_Dishes;
import com.innovative.androiddemo1.cls.Item_Rest;
import com.innovative.androiddemo1.cls.Restaurants;

import android.content.Context;
import android.util.Log;

public class C {
	Context context;
	String DateTimeFormat = "MMM d, yyyy hh:mm:ss aaa";

	C(Context context) {
		this.context = context;
	}

	public String logindata(String un, String pd) {
		String jd = jdata("Login.txt");
		jd = jd.replaceAll("@username", un);
		jd = jd.replaceAll("@pass", pd);
		return jd;

	}

	public String LongToTime(String time) {
		int i1 = time.indexOf('(', 0);
		int i2 = time.indexOf('+', 0);
		time = time.substring(i1 + 1, i2);
		return convertTime(time);
	}

	public String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}

	public String convertTime(String time) {
		String str = "";
		long yourmilliseconds = Long.parseLong(time);
		SimpleDateFormat sdf = new SimpleDateFormat(DateTimeFormat);
		Date resultdate = new Date(yourmilliseconds);
		str = sdf.format(resultdate);
		return str;
	}

	public String jdata(String name) {
		String jsondata = "";
		try {
			InputStream is = context.getAssets().open(name);
			int size = is.available();
			byte[] buffer = new byte[size];
			is.read(buffer);
			is.close();
			jsondata = new String(buffer);
			buffer = null;
		} catch (Exception e) {
			// e.printStackTrace();
			return e.getMessage();
		}

		return jsondata;
	}

	public void ParseJSN(String json) {
		// Restaurants res = new Restaurants();
		// Item_Rest itm = new Item_Rest();
		// Addresses ads = new Addresses();
		// String temp = "";
		try {
			JSONObject jsonObj = new JSONObject(json);
			// String tot_count = jsonObj.getString("TotalCount");
			// Log.v("tot_count", tot_count);
			JSONObject rest = jsonObj.getJSONObject("Restaurants");
			JSONObject cat1 = jsonObj.getJSONObject("Categories");
			JSONObject dishes = jsonObj.getJSONObject("Dishes");
			JSONObject cat_dis = jsonObj.getJSONObject("CategoryDishes");
			// String rest_count = rest.getString("TotalCount");
			// String last = rest.getString("LastItemTimeStamp");
			ParshRest(rest);
			ParshCat(cat1);
			ParshDishes(dishes);
			ParshCatDish(cat_dis);
			// res.setTotalCount(rest.getString("TotalCount"));
			// Log.v("TotalCount", res.getTotalCount());
			//
			// res.setLastTime(rest.getString("LastItemTimeStamp"));
			// Log.v("LastItemTimeStamp", res.getLastTime());
			//
			// JSONArray items = rest.getJSONArray("Items");
			// // Log.v("items", items.toString());
			//
			// for (int i = 0; i < items.length(); i++) {
			// itm.setID(items.getJSONObject(i).getString("ID"));
			// itm.setName(items.getJSONObject(i).getString("Name"));
			// itm.setType(items.getJSONObject(i).getString("Type"));
			// itm.setTypeID(items.getJSONObject(i).getString("TypeID"));
			//
			// Log.v("Item ID", itm.getID());
			// Log.v("Item Name", itm.getName());
			// Log.v("Item Type", itm.getType());
			// Log.v("Item TypeID", itm.getTypeID());
			// JSONObject address = items.getJSONObject(i).getJSONObject(
			// "Address");
			// ads.setID(address.getString("ID"));
			// ads.setCity(address.getString("City"));
			// ads.setStreetName(address.getString("StreetName"));
			// ads.setStreetNumber(address.getString("StreetNumber"));
			// ads.setLatitude(address.getString("Latitude"));
			// ads.setLongitude(address.getString("Longitude"));
			//
			// Log.v("Address ID", ads.getID());
			// Log.v("Address city", ads.getCity());
			// Log.v("Address Str name", ads.getStreetName());
			// Log.v("Address Str no", ads.getStreetNumber());
			// Log.v("Address Lat", ads.getLatitude());
			// Log.v("Address Long", ads.getLongitude());
			// itm.setAddress(ads);
			// }
			// res.setItem(itm);
			// Constants.restaurants = res;

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void ParshCatDish(JSONObject cat_dis) {
		CategoryDishes cd = new CategoryDishes();
		ArrayList<Item_CatDish> itms = new ArrayList<Item_CatDish>();

		try {
			cd.setTotalCount(cat_dis.getString("TotalCount"));
			Log.v("Cat TotalCount", cd.getTotalCount());
			String time = LongToTime(cat_dis.getString("LastItemTimeStamp"));
			cd.setLastTime(time);
			Log.v("Cat LastItemTimeStamp", cd.getLastTime());

			JSONArray items = cat_dis.getJSONArray("Items");
			for (int i = 0; i < items.length(); i++) {
				Item_CatDish itm = new Item_CatDish();

				itm.setCategoryID(items.getJSONObject(i)
						.getString("CategoryID"));
				itm.setDishID(items.getJSONObject(i).getString("DishID"));
				itm.setPrice(items.getJSONObject(i).getString("Price"));
				itm.setThumbnailUrl(items.getJSONObject(i).getString(
						"ThumbnailUrl"));
				itm.setDeleted(items.getJSONObject(i).getString("Deleted"));
				Log.v("CategoryID" + i, itm.getCategoryID());
				Log.v("DishID" + i, itm.getDishID());
				Log.v("Price" + i, itm.getPrice());
				Log.v("ThumbnailUrl" + i, itm.getThumbnailUrl());
				Log.v("Deleted " + i, itm.getDeleted());
				itms.add(itm);
			}
			cd.setItems(itms);
			Constants.cat_dishes = cd;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void ParshDishes(JSONObject dishes) {
		Dishes di = new Dishes();
		ArrayList<Item_Dishes> itms = new ArrayList<Item_Dishes>();

		try {
			di.setTotalCount(dishes.getString("TotalCount"));
			Log.v("Cat TotalCount", di.getTotalCount());
			String time = LongToTime(dishes.getString("LastItemTimeStamp"));
			di.setLastTime(time);
			Log.v("Cat LastItemTimeStamp", di.getLastTime());
			JSONArray items = dishes.getJSONArray("Items");
			for (int i = 0; i < items.length(); i++) {
				Item_Dishes itm = new Item_Dishes();

				itm.setID(items.getJSONObject(i).getString("ID"));
				itm.setName(items.getJSONObject(i).getString("Name"));
				itm.setIngredients(items.getJSONObject(i).getString(
						"Ingredients"));
				itm.setDeleted(items.getJSONObject(i).getString("Deleted"));
				Log.v("Item ID " + i, itm.getID());
				Log.v("Item Name " + i, itm.getName());
				Log.v("Item Ingredients " + i, itm.getIngredients());
				Log.v("Item Deleted " + i, itm.getDeleted());
				itms.add(itm);
			}
			di.setItems(itms);
			Constants.dishes = di;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void ParshCat(JSONObject cat1) {
		Categories cat = new Categories();
		ArrayList<Item_Cat> itms = new ArrayList<Item_Cat>();

		try {
			cat.setTotalCount(cat1.getString("TotalCount"));
			Log.v("Cat TotalCount", cat.getTotalCount());
			String time = LongToTime(cat1.getString("LastItemTimeStamp"));
			cat.setLastTime(time);
			Log.v("Cat LastItemTimeStamp", cat.getLastTime());

			JSONArray items = cat1.getJSONArray("Items");
			for (int i = 0; i < items.length(); i++) {
				Item_Cat itm = new Item_Cat();
				itm.setID(items.getJSONObject(i).getString("ID"));
				itm.setName(items.getJSONObject(i).getString("Name"));
				itm.setRestaurantID(items.getJSONObject(i).getString(
						"RestaurantID"));

				Log.v("Item ID " + i, itm.getID());
				Log.v("Item Name " + i, itm.getName());
				Log.v("Item Type " + i, itm.getRestaurantID());
				itms.add(itm);
			}
			cat.setItems(itms);
			Constants.categories = cat;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void ParshRest(JSONObject rest) {
		Restaurants res = new Restaurants();
		ArrayList<Item_Rest> itms = new ArrayList<Item_Rest>();
		Addresses ads = new Addresses();
		try {
			res.setTotalCount(rest.getString("TotalCount"));
			Log.v("Rest TotalCount", res.getTotalCount());
//			String time = LongToTime(rest.getString("LastItemTimeStamp"));
			int i1 = rest.getString("LastItemTimeStamp").indexOf('(', 0);
			int i2 = rest.getString("LastItemTimeStamp").indexOf('+', 0);
			String time = rest.getString("LastItemTimeStamp").substring(i1 + 1, i2);
			res.setLastTime(time);
			Log.v("Rest LastItemTimeStamp", res.getLastTime());

			JSONArray items = rest.getJSONArray("Items");
			// Log.v("items", items.toString());

			for (int i = 0; i < items.length(); i++) {
				Item_Rest itm = new Item_Rest();
				itm.setID(items.getJSONObject(i).getString("ID"));
				itm.setName(items.getJSONObject(i).getString("Name"));
				itm.setType(items.getJSONObject(i).getString("Type"));
				itm.setTypeID(items.getJSONObject(i).getString("TypeID"));

				Log.v("Item ID", itm.getID());
				Log.v("Item Name", itm.getName());
				Log.v("Item Type", itm.getType());
				Log.v("Item TypeID", itm.getTypeID());
				JSONObject address = items.getJSONObject(i).getJSONObject(
						"Address");
				ads.setID(address.getString("ID"));
				ads.setCity(address.getString("City"));
				ads.setStreetName(address.getString("StreetName"));
				ads.setStreetNumber(address.getString("StreetNumber"));
				ads.setLatitude(address.getString("Latitude"));
				ads.setLongitude(address.getString("Longitude"));

				Log.v("Address ID", ads.getID());
				Log.v("Address city", ads.getCity());
				Log.v("Address Str name", ads.getStreetName());
				Log.v("Address Str no", ads.getStreetNumber());
				Log.v("Address Lat", ads.getLatitude());
				Log.v("Address Long", ads.getLongitude());
				itm.setAddress(ads);
				itms.add(itm);
			}
			res.setItems(itms);
			Constants.restaurants = res;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
